#include "terminalDeviceTable.h"
#include "db.h"
#include "logger.h"
#include "version.h"
#include <string.h>
#include <stdlib.h>


#define TERMINAL_DEVICE_TABLE_NAME "t_terminal_device"
#define SQL_TERMINAL_DEVICE_TABLE_CREATE "CREATE TABLE IF NOT EXISTS " TERMINAL_DEVICE_TABLE_NAME " (id INT PRIMARY KEY NOT NULL, name TEXT NOT NULL, \
productID INT, parentID INT, protocolID INT, interfaceID INT, nodeID TEXT, addr TEXT NOT NULL, timeout INT, other TEXT, desc TEXT)"
#define SQL_TERMINAL_DEVICE_TABLE_INSERT(...) "INSERT INTO " TERMINAL_DEVICE_TABLE_NAME "(id, name, productID, parentID, protocolID, interfaceID, nodeID, addr, timeout, other, desc) VALUES (" #__VA_ARGS__ ")"
#define SQL_TERMINAL_DEVICE_TABLE_REPLACE(...) "REPLACE INTO " TERMINAL_DEVICE_TABLE_NAME "(id, name, productID, parentID, protocolID, interfaceID, nodeID, addr, timeout, other, desc) VALUES (" #__VA_ARGS__ ")"
#define SQL_TERMINAL_DEVICE_TABLE_DELETE(id) "DELETE FROM " TERMINAL_DEVICE_TABLE_NAME " WHERE id = " #id
#define SQL_TERMINAL_DEVICE_TABLE_SELECT(id) "SELECT * FROM " TERMINAL_DEVICE_TABLE_NAME " WHERE id = " #id



/*===============================================================*/
/* 终端设备 */

int createTerminalDeviceTable(){
    return db_create_table(SQL_TERMINAL_DEVICE_TABLE_CREATE);
}

int insertTerminalDevice(T_TERMINAL_DEVICE* device){
    if(NULL == device){
        log_file_e(APPNAME, "insert terminal device is NULL");
        return -1;
    }
    return db_insert(SQL_TERMINAL_DEVICE_TABLE_INSERT(device->id, device->name, interface->addr, interface->port, interface->isUsed, interface->ifType, interface->params));
}

int replaceTerminalDevice(T_TERMINAL_DEVICE* device){
    if(NULL == device){
        log_file_e(APPNAME, "replace terminal device is NULL");
        return -1;
    }
    return db_insert(SQL_TERMINAL_DEVICE_TABLE_REPLACE(device->id, device->name, interface->addr, interface->port, interface->isUsed, interface->ifType, interface->params));
}

int deleteTerminalDevice(T_TERMINAL_DEVICE* device){
    if(NULL == device){
        log_file_e(APPNAME, "delete terminal device is NULL");
        return -1;
    }
    return db_delete(SQL_TERMINAL_DEVICE_TABLE_DELETE(device->id));
}

static int terminalDeviceCallback(void *data, int argc, char **argv, char **azColName){
    int i = 0;
    if(NULL == data){
        log_file_e(APPNAME, "TerminalDeviceCallback data is NULL");
        return -1;
    }
    T_TERMINAL_DEVICE_LIST* devList = (T_TERMINAL_DEVICE_LIST*)data;
    if((0 == devList->count) || (NULL == devList->head)){
        devList->head = devList->tail = (T_TERMINAL_DEVICE*)malloc(sizeof(T_TERMINAL_DEVICE));
        devList->head->next = NULL;
        devList->head->prev = NULL;
    } else {
        devList->tail->next = (T_TERMINAL_DEVICE*)malloc(sizeof(T_TERMINAL_DEVICE));
        devList->tail->next->prev = devList->tail;
    }
    devList->count++;
    /*
int64_t parentID;
    int64_t productID;
    int64_t protocolID;
    int64_t interfaceID;
    char    nodeID[32];
    char    addr[32];
    int     timeout;
    char    other[256];
    char    desc[256];*/
    for(i = 0; i < argc; i++){
        if(0 == strcmp(azColName[i], "id")){
            devList->tail->id = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "name")){
            if(NULL == argv[i]){
                devList->tail->name[0] = '\0';
            } else {
                strcpy(devList->tail->name, argv[i]);
            }
        } else if(0 == strcmp(azColName[i], "parentID")){
            devList->tail->parentID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "productID")){
            devList->tail->productID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "protocolID")){
            devList->tail->protocolID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "interfaceID")){
            devList->tail->interfaceID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "addr")){
            if(NULL == argv[i]){
                devList->tail->addr[0] = '\0';
            } else {
                strcpy(devList->tail->addr, argv[i]);
            }
        } else if(0 == strcmp(azColName[i], "timeout")){
            devList->tail->timeout = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "other")){
            if(NULL == argv[i]){
                devList->tail->other[0] = '\0';
            } else {
                strcpy(devList->tail->other, argv[i]);
            }
        } else if(0 == strcmp(azColName[i], "desc")){
            if(NULL == argv[i]){
                devList->tail->desc[0] = '\0';
            } else {
                strcpy(devList->tail->desc, argv[i]);
            }
        }
        log_i("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return devList->count;
}

int selectTerminalDevice(const char* sql, T_TERMINAL_DEVICE_LIST* devList){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == devList)){
        log_file_e(APPNAME, "select terminal device sql is NULL");
        return -1;
    }
    return db_select(sql, terminalDeviceCallback, devList);

}
//
void dbTerminalDeviceInit(){
    createTerminalDeviceTable();
}
